/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.repository.model.project;


import cn.ac.nercis.pes.common.utils.JsonUtils;
import cn.ac.nercis.pes.common.constant.project.SecurityLevel;
import cn.ac.nercis.pes.common.constant.project.SystemState;
import cn.ac.nercis.pes.repository.constant.BaseConstants;
import cn.ac.nercis.pes.repository.converter.project.SecurityLevelConverter;
import cn.ac.nercis.pes.repository.converter.project.SystemStateConverter;
import cn.ac.nercis.pes.repository.model.base.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import lombok.*;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.GenericGenerator;

import java.time.LocalDateTime;

/**
 * 系统信息实体类
 * @author zpy
 * @version 1.0.0
 */
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "pes_system")
public class SystemDO extends BaseEntity {
    @Id
    @GenericGenerator(name = "generator",strategy = BaseConstants.CUSTOM_UUID_STRATEGY)
    @GeneratedValue(generator = "generator")
    @Column(name = "id",unique = true,nullable = false,length = 36)
    private String id;

    @Column(name = "name",nullable = false,length = 128)
    private String name;

    @Convert(converter = SecurityLevelConverter.class)
    @Column(name = "security_level",nullable = false)
    private SecurityLevel securityLevel;

    @Column(name = "s_level")
    private Integer sLevel;

    @Column(name = "a_level")
    private Integer aLevel;

    @Column(name = "g_level")
    private Integer gLevel;

    @Column(name = "req_level")
    private String reqLevel;

    @Column(name = "is_filing")
    private Boolean isFiling;

    @Column(name = "filing_number",length = 128)
    private String filingNumber;

    @Convert(converter = SystemStateConverter.class)
    @Column(name = "state",nullable = false)
    private SystemState state;

    @Column(name = "finish_date")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    protected LocalDateTime finishDate;

    @Column(length = 128)
    private String decision;

    @Column(length = 2000)
    private String desc;

    @Column(name = "sort",nullable = false)
    private Integer sort;

    @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)
    @JoinColumn(name = "project_id")
    private ProjectDO project;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof SystemDO systemDO)) {
            return false;
        }

        return new EqualsBuilder()
                .append(id, systemDO.id)
                .append(name, systemDO.name)
                .append(securityLevel, systemDO.securityLevel)
                .append(sLevel, systemDO.sLevel)
                .append(aLevel, systemDO.aLevel)
                .append(gLevel, systemDO.gLevel)
                .append(reqLevel, systemDO.reqLevel)
                .append(isFiling, systemDO.isFiling)
                .append(filingNumber, systemDO.filingNumber)
                .append(state, systemDO.state)
                .append(finishDate, systemDO.finishDate)
                .append(decision, systemDO.decision)
                .append(desc, systemDO.desc)
                .append(sort, systemDO.sort)
                .append(project, systemDO.project)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id)
                .append(name)
                .append(securityLevel)
                .append(sLevel)
                .append(aLevel)
                .append(gLevel)
                .append(reqLevel)
                .append(isFiling)
                .append(filingNumber)
                .append(state)
                .append(finishDate)
                .append(decision)
                .append(desc)
                .append(sort)
                .append(project)
                .toHashCode();
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName().concat(JsonUtils.toJson(this));
    }
}